﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Permissions;
using System.Web.Mvc;

namespace System.Web.Mvc
{
    //http://blog.maartenballiauw.be/post/2008/06/Code-based-ASPNET-MVC-GridView.aspx
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    public static class GridViewExtensions
    {
        // add sort column
        public static void GridView<T>(
            this HtmlHelper html,
            GridViewData<T> data,
            Action<GridViewData<T>> headerTemplate,
            Action<T, string> itemTemplate,
            string cssClass,
            string cssAlternatingClass,
            Action<T> editItemTemplate,
            Action<GridViewData<T>> footerTemplate)
        {
            headerTemplate(data);

            int i = 0;
            foreach (var item in data.PagedList)
            {
                if (!item.Equals(data.EditItem))
                {
                    itemTemplate(item, (i % 2 == 0 ? cssClass : cssAlternatingClass));
                }
                else
                {
                    editItemTemplate(item);
                }

                i++;
            }

            footerTemplate(data);
        }
    }
}